﻿#include "WoomaTaurus.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Map.h"
#include "../../../Shared/Data/Stat.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;

namespace Server::MirObjects::Monsters
{

    WoomaTaurus::WoomaTaurus(MonsterInfo *info) : FlamingWooma(info)
    {
    }

    void WoomaTaurus::ProcessAI()
    {
        if (Dead)
        {
            return;
        }

        if (_madTime > 0 && getEnvir()->getTime() > _madTime)
        {
            _madTime = 0;
            RefreshAll();
        }

        if (getEnvir()->getTime() > _teleTime)
        {
            _teleTime = getEnvir()->getTime() + TeleDelay;

            int count = 0;
            MirDirection dir = getDirection();

            for (int i = 0; i < 8; i++)
            {
                Point *location = Functions::PointMove(getCurrentLocation(), dir, 1);

                if (getCurrentMap()->ValidPoint(location))
                {
                    Cell *cell = getCurrentMap()->GetCell(location);

                    if (cell->Objects.empty())
                    {
                        continue;
                    }

                    for (int o = 0; o < cell->Objects.size(); o++)
                    {
                        if (!cell->Objects[o]->getBlocking())
                        {
                            continue;
                        }
                        count++;
                        break;
                    }
                }
                else
                {
                    count++;
                }

                dir = Functions::NextDir(dir);
            }

            if (count >= 5)
            {
                setTarget(nullptr);
                TeleportRandom(4, 0);
            }
        }


        if (Stats[Stat::HP] >= 7)
        {
            unsigned char stage = static_cast<unsigned char>(HP / (Stats[Stat::HP] / 7));

            if (stage < _stage)
            {
                _madTime = getEnvir()->getTime() + 8000;
                MoveSpeed = 400;
                AttackSpeed = 500;
            }
            _stage = stage;
        }


        FlamingWooma::ProcessAI();
    }
}
